/*
 * FileName：VoteCtrl.java 
 * <p>
 * Copyright (c) 2017-2020 <a href="https://www.smartwx.info">hermit(1154808491@qq.com)</a>.
 * <p>
 * Licensed under the GNU General Public License, Version 3 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/gpl-3.0.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package com.smart.wx.wxcms.ctrl;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.jpay.ext.kit.ZxingKit;
import com.smart.common.BaseCtrl;
import com.smart.common.util.AjaxResult;
import com.smart.common.util.PropertiesUtil;
import com.smart.common.util.Util;
import com.smart.common.util.WxMemoryCacheClient;
import com.smart.wx.wxcms.entity.Vote;
import com.smart.wx.wxcms.service.VoteService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author hermit
 * @version 2.0
 * @date 2018-07-10 23:25:20
 */
@Controller
@RequestMapping("/vote")
public class VoteCtrl extends BaseCtrl {
    @Resource
    private VoteService voteService;

    /**
     * 列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public AjaxResult list(Vote vote) throws Exception {

        String account = WxMemoryCacheClient.getMpAccount().getAccount();
        vote.setAccount(account);

        List<Vote> list = voteService.queryListByPage(vote);
        return getResult(vote, list);
    }

    /**
     * 信息
     */
    @ResponseBody
    @RequestMapping("/info")
    public AjaxResult info(Vote vote) throws Exception {
        Vote one = voteService.queryObject(vote);
        return AjaxResult.success(one);
    }

    /**
     * 保存
     */
    @ResponseBody
    @RequestMapping("/save")
    public AjaxResult save(Vote vote) throws Exception {

        String account = WxMemoryCacheClient.getMpAccount().getAccount();
        vote.setAccount(account);

        String applyUrl = "";
        String voteUrl = "";

        if (vote.getId() == null || vote.getId().equals("")) { // 新增
            vote.setId(Util.getUUID());
            this.getApplyUrl(vote);
            this.getVoteUrl(vote);
            vote.setApplyUrl(applyUrl);
            vote.setVoteUrl(voteUrl);
            vote.setCreatetime(new Date());
            vote.setUpdatetime(new Date());
            vote.setVoteState(0);
            this.voteService.insert(vote);
        } else {
            this.getApplyUrl(vote);
            this.getVoteUrl(vote);
            vote.setApplyUrl(applyUrl);
            vote.setVoteUrl(voteUrl);
            vote.setUpdatetime(new Date());
            this.voteService.update(vote);
        }

        return AjaxResult.saveSuccess();
    }


    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    public AjaxResult update(Vote vote) throws Exception {
        voteService.update(vote);
        return AjaxResult.updateSuccess();
    }

    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping("/delete")
    public AjaxResult delete(Vote vote) throws Exception {
        voteService.delete(vote);
        return AjaxResult.deleteSuccess();
    }

    /**
     * 发布
     */
    @ResponseBody
    @RequestMapping("/release")
    public AjaxResult release(Vote vote) throws Exception {
        vote = voteService.queryObject(vote);
        vote.setVoteState(1);
        voteService.update(vote);
        return AjaxResult.success();
    }

    /**
     * 查看链接
     */
    @ResponseBody
    @RequestMapping("/viewUrl")
    public AjaxResult viewUrl(Vote vote) throws Exception {
        Vote one = voteService.queryObject(vote);

        /***** 生成二维码 **********/
        String account = WxMemoryCacheClient.getMpAccount().getAccount();
        String fileDir = PropertiesUtil.getString("res.upload.path");
        String resURL = PropertiesUtil.getString("res.upload.url");

        // 生成二维码(投票页面)
        String fileName_vote = account + "_" + one.getId() + "_vote.png";
        String filePath_vote = fileDir + fileName_vote;
        ZxingKit.encode(one.getVoteUrl(), BarcodeFormat.QR_CODE, 3, ErrorCorrectionLevel.H, "png", 200, 200, filePath_vote);
        String voteUrl = resURL + fileName_vote;
        one.setVoteQRCodeUrl(voteUrl);

        // 生成二维码(报名页面)
        String fileName_apply = account + one.getId() + "_apply.png";
        String filePath_apply = fileDir + fileName_apply;
        ZxingKit.encode(one.getApplyUrl(), BarcodeFormat.QR_CODE, 3, ErrorCorrectionLevel.H, "png", 200, 200, filePath_apply);
        String applyUrl = resURL + fileName_apply;
        one.setApplyQRCodeUrl(applyUrl);

        return AjaxResult.success(one);
    }

    /**
     * 获取报名url
     *
     * @return
     */
    private String getApplyUrl(Vote vote) {
        // 微信报名页面
//        String path = SpringFreemarkerContextPathUtil.getBasePath(request);
//        String redirectUrl_apply = request.getScheme() + "://" + request.getServerName() + path + "/front/voteindex/getVoteApplyIndex/" + WxMemoryCacheClient.getAccount() + "/" + vote.getId();

        String domainName = PropertiesUtil.getString("domain.name");
        String redirectUrl_apply = domainName + "/front/voteindex/getVoteApplyIndex/" + WxMemoryCacheClient.getAccount() + "/" + vote.getId();

        return redirectUrl_apply;
    }

    /**
     * 获取投票url
     *
     * @return
     */
    private String getVoteUrl(Vote vote) {
        // 微信投票页面
//        String path = SpringFreemarkerContextPathUtil.getBasePath(request);
//        String redirectUrl_voteRecord = request.getScheme() + "://" + request.getServerName()+path+"/front/voteindex/getVoteIndex/" + WxMemoryCacheClient.getAccount() + "/" + vote.getId();

        String domainName = PropertiesUtil.getString("domain.name").toString();
        String redirectUrl_voteRecord = domainName + "/front/voteindex/getVoteIndex/" + WxMemoryCacheClient.getAccount() + "/" + vote.getId();

        return redirectUrl_voteRecord;
    }

}
